package com.ym.manager.admin.goods;

import com.ym.manager.admin.goods.model.Goods;
import com.ym.manager.admin.merchant.MerchantManager;
import com.ym.manager.admin.merchant.model.Merchant;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.Common;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * @program: ym-admin->GoodsController
 * @Auther: 但何非
 * @Date: 2019/11/21 14:39
 * @Description: 商品
 */
@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 页面：产品列表
     * @return
     */
    @RequestMapping("list")
    public String index(Model model,@CurrentUser AdminUser user){
        model.addAttribute("categoryList",goodsCategoryService.getCategoryList(0));
        return "/goods/list";
    }

    /**
     * 功能：产品列表
     * @param user
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public String getList(@CurrentUser AdminUser user,
                          @RequestParam(defaultValue = "0") int page,
                          @RequestParam(defaultValue = "10") int limit,
                          @RequestParam(defaultValue = "") String name,
                          @RequestParam(defaultValue = "-1") int categoryPid,
                          @RequestParam(defaultValue = "-1") int categoryId,
                          @RequestParam(defaultValue = "-1") int status){
        Map<String,Object> map = ResultUtil.result();
        try {
            Pages<Goods> pages = goodsService.getList(page,limit,user.getMerchant_id(),name,categoryPid,categoryId,status);
            //查询数据
            map.put("data",pages.getList());
            map.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 功能：获取各个状态的产品总数
     * @return
     */
    @RequestMapping("goodsStatusTotal")
    @ResponseBody
    public String goodsStatusTotal(@CurrentUser AdminUser user){
        Map<String,Object> result = ResultUtil.result();
        try {
            //查询数据
            result.put("data",goodsService.goodsStatusTotal(user.getMerchant_id()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 页面：新增商品
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String addGoods(Model model,@CurrentUser AdminUser user){
        model.addAttribute("categoryList",goodsCategoryService.getCategoryList(0));
        model.addAttribute("maxIdx",goodsService.getMaxIdx());
        return "/goods/add";
    }

    /**
     * 功能：新增商品
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public String addGoods(@ModelAttribute Goods goods,@CurrentUser AdminUser user){
        Map<String,Object> map = ResultUtil.result();
        goods.setMerchant_id(user.getMerchant_id());
//        Merchant merchant = merchantManager.getMerchant(user.getMerchant_id());
//        if(merchant!=null){
//            goods.setMerchant_name(merchant.getName());
//        }
        goods.setStatus((byte)1);
        goods.setType(1);
        goods.setUnits(goods.getUnits().replaceAll("，",","));
        //添加商品
        int i = goodsService.insert(goods);
        if(i>0){
            map.put("msg","添加成功");
        }else if (i==-1){
            map.put("code",i);
            map.put("msg","添加错误，请联系管理人员");
        }else{
            map.put("code",i);
            map.put("msg","添加失败");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 页面：修改商品
     * @param model
     * @param id
     * @return
    */
    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public String editGoods(@CurrentUser AdminUser user,Model model,int id){
        model.addAttribute("goods",goodsService.detail(id));
        model.addAttribute("categoryList",goodsCategoryService.getCategoryList(0));
        return "/goods/edit";
    }

    /**
     * 功能：修改商品
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public String editGoods(@CurrentUser AdminUser user,@ModelAttribute Goods goods){
        Map<String,Object> map = ResultUtil.result();
//        goods.setMerchant_id(user.getMerchant_id());
        goods.setUnits(goods.getUnits().replaceAll("，",","));
        int i = goodsService.update(goods);
        if(i>0){
            map.put("msg","修改成功");
        }else if (i==-1){
            map.put("code",i);
            map.put("msg","修改错误，请联系管理人员");
        }else{
            map.put("code",i);
            map.put("msg","修改失败");
        }
        return ResultUtil.toJSON(map);
    }


    /**
     * 功能：上下架
     * @param id
     * @param shelf
     * @return
     */
    @RequestMapping("shelf")
    @ResponseBody
    public String shelf(@RequestParam(defaultValue = "0") int id,
                        @RequestParam(defaultValue = "-1") int shelf){
        Map<String,Object> reuslt = ResultUtil.result();
        if(id<=0 || shelf<0){
            reuslt.put("code",-1);
            reuslt.put("msg","请求参数异常");
            return ResultUtil.toJSON(reuslt);
        }
        try {
            int i = goodsService.shelf(id,shelf);
            reuslt.put("code",i>0?0:-2);
            reuslt.put("msg",i>0?shelf==1?"上架成功":"下架成功":shelf==1?"上架失败，请联系管理员":"下架失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            reuslt.put("code",-1000);
            reuslt.put("msg","系统异常,请联系管理员");
        }
        return ResultUtil.toJSON(reuslt);
    }

    /**
     * 功能：商品入库
     * @return
     */
    @RequestMapping(value = "addInventory",method = RequestMethod.POST)
    @ResponseBody
    public String addInventory(@RequestParam(defaultValue = "0") int goods_id,
                               @RequestParam(defaultValue = "") String validity,
                               @RequestParam(defaultValue = "0") int inventory){
        Map<String, Object> result = ResultUtil.result();
        if(goods_id <= 0 || Common.isEmpty(validity) || inventory <= 0){
            result.put("code",-1);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = goodsService.addInventory(goods_id,validity,inventory);
            result.put("code",i>0?0:-2);
            result.put("msg",i>0?"入库成功":"入库失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常,请联系管理员");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：加入秒杀
     * @return
     */
    @RequestMapping(value = "getSeckillTime",method = RequestMethod.GET)
    @ResponseBody
    public String getSeckillTime(@RequestParam(defaultValue = "0") int goods_id){
        Map<String, Object> result = ResultUtil.result();
        if(goods_id <= 0){
            result.put("code",-1);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            result.put("data",goodsService.getSeckillTime(goods_id));
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常,请联系管理员");
            result.put("data",new ArrayList<>());
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：加入秒杀
     * @return
     */
    @RequestMapping(value = "addSeckill",method = RequestMethod.POST)
    @ResponseBody
    public String addSeckill(@RequestParam(defaultValue = "0") int goods_id,
                             @RequestParam(defaultValue = "") String unit,
                             @RequestParam(defaultValue = "") String time){
        Map<String, Object> result = ResultUtil.result();
        if(goods_id <= 0 || Common.isEmpty(unit) || Common.isEmpty(time)){
            result.put("code",-1);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = goodsService.addSeckill(goods_id,unit,time);
            result.put("code",i>0?0:-2);
            result.put("msg",i>0?"入库成功":"入库失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常,请联系管理员");
        }
        return ResultUtil.toJSON(result);
    }
//
//    /**
//     * 功能：删除产品
//     * @return
//     */
//    @RequestMapping("/delete")
//    @ResponseBody
//    public String delete(@RequestParam(defaultValue = "0") int id){
//        Map<String,Object> map = ResultUtil.result();
//        int i = goodsService.delete(id);
//        if(i>0){
//            map.put("msg","删除成功");
//        }else if (i==-1){
//            map.put("code",i);
//            map.put("msg","删除错误，请联系管理人员");
//        }else{
//            map.put("code",i);
//            map.put("msg","删除失败");
//        }
//        return ResultUtil.toJSON(map);
//    }
//
//    /**
//     * 根据类目获取产品列表
//     * @param categoryPid 父类目id
//     * @param categoryId 类目id
//     * @return
//     */
//    @RequestMapping("/getCategoryGoodsList")
//    @ResponseBody
//    public String getCategoryGoodsList(int categoryPid,
//                                       int categoryId,
//                                       @CurrentUser AdminUser user){
//        Map<String,Object> result = ResultUtil.result();
//        try {
//            result.put("data",goodsService.getCategoryGoodsList(user.getHospital_id(),categoryPid,categoryId));
//        }catch (Exception e){
//            e.printStackTrace();
//            result.put("code",-1000);
//            result.put("msg","系统异常,请联系管理员");
//        }
//        return ResultUtil.toJSON(result);
//    }
}
